<template>
  <div class="newAuction" :class="className">
    <div v-html="css"></div>
    <div class="list" v-if="datas.list">
      <div class="lis" v-for="item in good_list" :key="item.id" @click="tapDetail(item)">
        <div class="lis_img" v-if="item.has_one_goods">
          <img :src="item.has_one_goods.thumb" alt="" />
        </div>
        <div class="lis_b" v-if="item.has_one_goods">
          <div class="title">{{ item.has_one_goods.title }}</div>
          <div class="price">{{ $i18n.t("money") }}{{ item.now_amount }}</div>
          <div class="countDownt">
            <span class="txt">{{ item.running_status == 0 ? "开拍倒计时：" : "距离结束：" }}</span>
            <van-count-down :time="setTime(item)"> </van-count-down>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
// 自定义样式
const css = function() {
  // 确保数据已传入
  const { preview_color } = this.datas;
  return `
      .component-${this.id} .list {
        background-color: ${preview_color.color};
        background-image: url(${preview_color.isColor == "2" ? preview_color.image : ""});
        background-size: 100% 100%;
        background-position: center;
        background-repeat: no-repeat;
      }
  `;
};
export default {
  props: ["id", "datas", "tabcontrol", "lastIndex", "index", "plugin_active", "isBottom"],
  data() {
    return {
      good_list: []
    };
  },
  computed: {
    css() {
      return "<style>" + css.call(this) + "</style>";
    },
    className() {
      const name = ["component-wrapper", `component-${this.id}`];
      return name;
    },
    setTime(item) {
      return function(item) {
        let nowTime = new Date().getTime();
        let rtime = "";
        if (item.running_status == 0) {
          // 未开始
          console.log(item.start_second, nowTime);
          rtime = (item.start_second * 1000 - nowTime).toFixed(0);
        } else if (item.running_status == 1) {
          // 进行中
          rtime = (item.end_second * 1000 - nowTime).toFixed(0);
        }
        console.log(rtime, nowTime);
        return rtime;
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    tapDetail(item) {
      this.$router.push(this.fun.getUrl("new_auction_detail", { id: item.id }));
    },
    async getData() {
      if (this.datas.list.length <= 0) return;
      let ids = [];
      ids = this.datas.list.map(item => {
        return item.id;
      });
      console.log(this.datas.list, ids);
      let { result, data, msg } = await $http.post("plugin.new-auction.frontend.activity", { activity_ids: ids }, "");
      if (result) {
        this.good_list = data;
      } else {
        this.$toast(msg);
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.list {
  padding: 0.63rem 0.75rem;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  .lis {
    background: #fff;
    border-radius: 0.75rem;
    display: flex;
    flex-direction: column;
    // 设置宽度为50%，确保一行两个元素
    width: calc(50% - 0.315rem); // 减去一半的margin-right以避免换行时总宽度超出
    margin-right: 0.63rem;
    margin-bottom: 0.63rem; // 可选，增加底部外边距以改善视觉效果

    &:nth-child(2n) {
      margin-right: 0; // 每行第二个元素取消右边距
    }
    .lis_img {
      flex-shrink: 0;
      height: 10.66rem;
      background: #c1c1c1;
      border-radius: 0.75rem;
      img {
        width: 100%;
        height: 100%;
        border-radius: 0.75rem;
      }
    }
    .lis_b {
      flex: 1;
      overflow: hidden;
      padding: 0.63rem 0.75rem;
      box-sizing: border-box;
      text-align: left;
      .title {
        font-weight: bold;
        font-size: 0.81rem;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .countDownt {
        color: #f15353;
        font-size: 0.81rem;
        line-height: 0.81rem;
        display: flex;
        align-items: center;
        .txt {
          color: #3b3b4a;
        }
        .van-count-down {
          color: #f15353 !important;
        }
      }
      .price {
        font-weight: 500;
        font-size: 0.94rem;
        color: #f15353;
        line-height: 1rem;
        margin: 0.63rem 0 0.4rem 0;
      }
    }
  }
}
</style>
